package controllers
{
    import com.pamakids.services.QNService;
    
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.IOErrorEvent;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;
    
    import views.anime.AnimePlayer;
    import views.player.PaintPlayer;
    
    public class GlobalAudio extends EventDispatcher
    {
        public function GlobalAudio()
        {
        }
        
        private var snd:Sound;
        private var sndC:SoundChannel;
        
        private static var _instance:GlobalAudio;
        
        private var crtAudio:String='';
        
        public static function get instance():GlobalAudio
        {
            if(!_instance)
                _instance=new GlobalAudio();
            return _instance;
        }
        
        public function play(url:String,track:Boolean=true):void
        {
            if(crtAudio==url&&snd)
            {
                if(playing)
                    pause();
                else
                {
                    playing=true;
                    sndC=snd.play(pauseTime);
                }
//                dispose();
                return;
            }
            
            dispose();
            crtAudio=url;
            if(url.indexOf('msgHint')==-1)
                url=QNService.getFullAudio(url);
            
            if(track)
                EventTracer.instance.activePageEvent('播放录音',url);
            
            snd=new Sound(new URLRequest(url));
            snd.addEventListener(IOErrorEvent.IO_ERROR,onError);
            sndC=snd.play();
            sndC.addEventListener(Event.SOUND_COMPLETE,onComplete);
            playing=true;
        }
        
        public function playBA(ba:ByteArray,track:Boolean=true):void
        {
            dispose();
            
            ba.position = 0;
            snd=new Sound();
            snd.addEventListener(IOErrorEvent.IO_ERROR,onError);
            snd.loadCompressedDataFromByteArray(ba,ba.length);
            sndC=snd.play();
            sndC.addEventListener(Event.SOUND_COMPLETE,onComplete);
            playing=true;
        }
        
        private var playing:Boolean;
        
        private function pause():void
        {
            playing=false;
            pauseTime=sndC.position;
            sndC.stop();
        }
        
        private var pauseTime:Number;
        
        public function getPprogress(_url:String):Number
        {
            if(snd&&sndC&&_url==crtAudio)
                return sndC.position/snd.length;
            return 0;
        }
        
        public function dispose(stopPlayer:Boolean=true):void
        {
            pauseTime=0;
            playing=false;
            if(sndC)
            {
                snd.removeEventListener(IOErrorEvent.IO_ERROR,onError);
                sndC.stop();
                sndC=null;
            }
            if(snd)
            {
                snd.removeEventListener(IOErrorEvent.IO_ERROR,onError);
                snd=null;
            }
            
            crtAudio='';
            dispatchEvent(new Event('playComplete'));
        }
        
        protected function onComplete(event:Event):void
        {
            dispatchEvent(new Event('playComplete'));
            dispose();
        }
        
        protected function onError(event:IOErrorEvent):void
        {
            dispatchEvent(new Event('playComplete'));
            dispose();
        }
    }
}

